package com.zsguan.s.stools4andr.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2015/9/14.
 * File read manager
 */
public class ZFileReadManager {

    public static boolean read(String path, OnReadLineListener lineListener) throws IOException {
        FileReader isr = null;
        BufferedReader br = null;

        try {
            isr = new FileReader(new File(path));
            br = new BufferedReader(isr);

            String line;

            while ((line = br.readLine()) != null) {
                lineListener.onReadLine(line);
            }
        } finally {
            if (null != br) {
                br.close();
            }

            if (null != isr) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }


    /**
     * 从文件中每读取一行时 进行一次回调的接口
     */
    public interface OnReadLineListener {
        void onReadLine(String line) throws UnsupportedEncodingException;
    }
}
